package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.ChatActivity;
import com.xgkj.diyiketang.activity.UserDetailActivity;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.ViewUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by windo on 2015/12/18.
 */
public class STMemberAdapter extends BaseAdapter {

    private Context mContext;

    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();

    public STMemberAdapter(Context context) {
        mContext = context;
    }

    private int type;

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public void updateData(List<EatShowUserInfo> list) {
        if (mList != null) {
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData() {
        if (mList != null) {
            mList.clear();
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if(mList !=null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ChildHolder childHolder = null;
        if(convertView == null || convertView.getTag() == null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.st_member_item,parent,false);
            childHolder.headImg = (ImageView) convertView.findViewById(R.id.head_img);
            childHolder.nickNameText = (TextView) convertView.findViewById(R.id.nickname);
            childHolder.personSignatureText = (TextView) convertView.findViewById(R.id.person_signature);
            childHolder.sendMessageBtn = (ImageButton) convertView.findViewById(R.id.send_message);
            childHolder.jieshouBtn = (ImageButton) convertView.findViewById(R.id.receive_btn);
            convertView.setTag(childHolder);
        }else{
            childHolder = (ChildHolder) convertView.getTag();
        }

        final EatShowUserInfo  fansInfo = mList.get(position);
        if(fansInfo != null){
            String headImgUrl = fansInfo.getUserLogo();
            if(headImgUrl != null && headImgUrl.length()>0){
                ViewUtil.Size size =  ViewUtil.getHeadImageSize_40(mContext);
                Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(headImgUrl,size));
                if (cacheBitmap != null){
                    childHolder.headImg.setImageBitmap(cacheBitmap);
                }else {
                    childHolder.headImg.setImageResource(R.drawable.default_head_image);
                    ImageCache.getInstance(mContext).loadBitmapToView(childHolder.headImg, headImgUrl, size);
                }
                childHolder.headImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(mContext, UserDetailActivity.class);
                        intent.putExtra("userId",fansInfo.getUserId());
                        mContext.startActivity(intent);
                    }
                });
            }else{
                childHolder.headImg.setImageResource(R.drawable.default_head_image);
            }
            if(fansInfo.getNickName()!=null){
                try {
                    childHolder.nickNameText.setText(URLDecoder.decode(fansInfo.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            if(fansInfo.getUserSign()!=null){
                try {
                    childHolder.personSignatureText.setText(URLDecoder.decode(fansInfo.getUserSign(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

            if(type==1){
                childHolder.sendMessageBtn.setVisibility(View.VISIBLE);
                childHolder.sendMessageBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null){
                            mListener.onReceiveClick(fansInfo.getUserId());
                        }
                    }
                });
            }else if(type == 2){
                childHolder.jieshouBtn.setVisibility(View.VISIBLE);
                childHolder.jieshouBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null){
                            mListener.onReceiveClick(fansInfo.getUserId());
                        }
                    }
                });
            }
        }

        return convertView;
    }

    public interface OnAdapterClickListener{
        public void onReceiveClick(String userId);
    }

    private OnAdapterClickListener mListener;

    public void setOnAdapterClickListener(OnAdapterClickListener listener){
        mListener = listener;
    }

    class ChildHolder{
        ImageView headImg;
        TextView nickNameText;
        TextView personSignatureText;
        ImageButton sendMessageBtn;
        ImageButton jieshouBtn;
    }
}
